<?php

namespace app\controllers\supplier\uni\finance;

use app\controllers\supplier\AdminController;
use app\models\order\Order;
use app\models\pay\Pay;
use app\models\supplier\SupplierBank;
use app\models\supplier\SupplierBank as Model;
use app\models\supplier\SupplierCash;
use app\models\system\SetBasic;

class AccountController extends AdminController
{
    public function actionFetch()
    {
        $model = Model::getLastOne($this->user->id);

        return $this->success($model);
    }

    public function actionSave()
    {
        $data = $this->post();

        $data['user_id'] = $this->user->id;

        if (Model::saveData($data)) {
            return $this->success();
        }

        return $this->fail();
    }

    // 提现
    public function actionCash()
    {
        $data = $this->post();

        $set = SetBasic::getLastOne();

        // 总进帐流水, 提现完成总数
        // $order_sn = Order::find()
        //     ->where(['supplier_id' => $this->user->id])
        //     ->select(['sn'])
        //     ->column();
        // $total_amount = Pay::find()
        //     ->where(['status' => 1])
        //     ->andWhere(['sn' => $order_sn])
        //     ->sum('amount');
        $total_amount = Order::find()
            ->where(['supplier_id' => $this->user->id])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->sum('total_pay');

        $total_cash = SupplierCash::find()
            ->where(['status' => 1])
            ->andWhere(['user_id' => $this->user->id])
            ->sum('amount');
        $total_cash_check = SupplierCash::find()
            ->where(['status' => 0])
            ->andWhere(['user_id' => $this->user->id])
            ->sum('amount');

        // mini
        if ($data['amount'] < $set->cash_min_money) {
            return $this->fail('提现金额错误');
        }

        // cash for day
        $count = SupplierCash::find()
            ->where(['user_id' => $this->user->id])
            ->andWhere(['DATE_FORMAT(create_time, "%d")' => date('d')])
            ->count();
        if ($count + 1 > $set->cash_total_for_day) {
            return $this->fail('提现次数超过,每天只能提现:' . intval($set->cash_total_for_day) . '次');
        }

        // balance
        if ($data['amount'] > ($total_amount - $total_cash - $total_cash_check)) {
            return $this->fail('余额不足');
        }

        // bank
        // if (empty($data['bank']) || !$data['bank']['bank_number'] || !$data['bank']['nickname']) {
        //     return $this->fail('请填写银行卡信息');
        // }
        $account = SupplierBank::getLastOne($this->user->id, true);
        if (!$account) {
            return $this->fail('您还没有绑定银行卡,请到帐户中心绑定');
        }

        $transaction = \Yii::$app->db->beginTransaction();

        try {
            // // account
            // $account->balance -= $data['amount'];
            // $account->save();

            // cash record
            $model = new SupplierCash();
            $model->sn = SupplierCash::generateSn();
            $model->amount = $data['amount'];
            $model->service = ($set->cash_service / 100) * $data['amount'];
            $model->user_id = $this->user->id;
            $model->status = 0;
            $model->nickname = $account['nickname'];
            $model->bank_number = $account['bank_number'];
            $model->bank_name = $account['bank_name'];
            $model->mobile = $account['mobile'];
            $model->save();

            $transaction->commit();
            return $this->success();
        } catch (\Exception $e) {
            $transaction->rollBack();
            return $this->fail($e->getMessage());
        }
    }
}